﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TUM.Mustertexte_Parser
{
    public class TemplateParser
    {
        private XmlDocument MustertexteDocument;
        private XmlNode ProductNode;
        private string TemplateFilePath;

        public string ProductName
        {
            get;
            private set;
        }

        private TemplateParser(string mustertexteDocumentPath)
            : this(mustertexteDocumentPath, null)
        { }

        private TemplateParser(string mustertexteDocumentPath, string productName)
        {
            if (mustertexteDocumentPath == null)
                throw new ArgumentNullException("mustertexteDocumentPath");

            TemplateFilePath = mustertexteDocumentPath;
            MustertexteDocument = new XmlDocument();
            MustertexteDocument.Load(mustertexteDocumentPath);
            

            if (productName != null)
            {
                ProductName = productName;
                ProductNode = MustertexteDocument.SelectSingleNode("Musterbibliothek/Mustergruppe[Name='" + ProductName + "']");
            }
        }

        private string[] GetTemplateProducts()
        {
            var productNameNodes = MustertexteDocument.SelectNodes("Musterbibliothek/Mustergruppe/Name");
            string[] productNames = new string[productNameNodes.Count];
            int a = 0;

            foreach (XmlNode curProductNameNode in productNameNodes)
            {
                productNames[a] = curProductNameNode.InnerText;
                a++;
            }
            return productNames;
        }

        public ProductTopic GetTopicByName(string topicName)
        {
            if (topicName == null) throw new ArgumentNullException("topicName");
            return GetTopicByQuery("Themenmuster[Name='" + topicName + "']");
        }

        public ProductTopic GetTopicById(string topicId)
        {
            if (topicId == null) throw new ArgumentNullException("topicId");
            return GetTopicByQuery("Themenmuster[VMThemaRef/@link='" + topicId + "']");
        }

        private ProductTopic GetTopicByQuery(string xPathQuery)
        {
            if (ProductNode == null) return null;

            XmlNode topicNode = ProductNode.SelectSingleNode(xPathQuery);
            if (topicNode == null)
                return null;
            return new ProductTopic(TemplateFilePath, topicNode);
        }

        public ProductTopic[] GetTopics()
        {
            if (ProductNode == null) return null;

            XmlNodeList topicNodes = ProductNode.SelectNodes("Themenmuster");
            ProductTopic[] topics = new ProductTopic[topicNodes.Count];
            int a = 0;

            foreach (XmlNode curTopicNode in topicNodes)
            {
                topics[a] = new ProductTopic(TemplateFilePath, curTopicNode);
                a++;
            }
            return topics;
        }


        public static TemplateParser CreateTemplateParser(string mustertexteFilePath, string productName)
        {
            if (productName == null) throw new ArgumentNullException("productName");

            return new TemplateParser(mustertexteFilePath, productName);
        }

        /// <summary>
        /// Gets the names of all products that have a belonging Mustertext.
        /// </summary>
        /// <param name="mustertexteFilePath">Path of the Mustertexte file.</param>
        /// <returns>Names of all products that have a belonging Mustertext.</returns>
        public static string[] GetTemplateProducts(string mustertexteFilePath)
        {
            var parser = new TemplateParser(mustertexteFilePath);
            return parser.GetTemplateProducts();
        }
    }
}
